/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.AccountDTO;
import east2west.dto.CarOrderDTO;
import east2west.dto.FunctionDTO;
import east2west.dto.TourDTO;
import east2west.dto.TourOrderDTO;
import east2west.service.AdminServicer;
import east2west.service.CarOrderService;
import east2west.service.TourService;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author anhtuan
 */
public class loginAction extends DispatchAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERR = "err";
    private static final String LOGOUT = "logout";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        String use = request.getParameter("usename");
        String pass = request.getParameter("pass");
        String mess = "";
        AdminServicer serAdmin = new AdminServicer();
        AccountDTO accDTO = serAdmin.findByUsename(use);
        if (accDTO == null) {
            request.setAttribute("mess", "thong tin tai khoan ko dung");
            return mapping.findForward(ERR);
        } else {
            if (accDTO.getPassWord().equals(pass)) {
                //liet ke thong tin function cua username
                ArrayList<FunctionDTO> listFunction = serAdmin.findFunctionByRoleID(accDTO.getRoleDTO().getRoleId());
                if (listFunction == null) {
                    mess += "ban ko co quyen vao trang nay";
                } else {
                    for (Iterator<FunctionDTO> it = listFunction.iterator(); it.hasNext();) {
                        FunctionDTO functionDTO = it.next();
                        if (functionDTO.getFunctionId() >= 1 && functionDTO.getFunctionId() <= 3) {
                            session.setAttribute("roleUser", "admin");
                        }
                        if (functionDTO.getFunctionId() >= 4 && functionDTO.getFunctionId() <= 6) {
                            session.setAttribute("roleUser", "car");
                            //hien thi thong tin cac order voi gia tri isdelete = false
                            CarOrderService serCarOrder = new CarOrderService();
                            ArrayList<CarOrderDTO> orderDTO = serCarOrder.readOrder();
                            request.setAttribute("orderDTO", orderDTO);
                        }
                        if (functionDTO.getFunctionId() >= 7 && functionDTO.getFunctionId() <= 9) {
                            session.setAttribute("roleUser", "tour");
                            TourService serTour = new TourService();
                            ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
                            request.setAttribute("orderTourDTO", orderTourDTO);
                        }
                        if (functionDTO.getFunctionId() >= 10 && functionDTO.getFunctionId() <= 12) {
                            session.setAttribute("roleUser", "hotel");
                        }
                        if (functionDTO.getFunctionId() >= 13 && functionDTO.getFunctionId() <= 15) {
                            session.setAttribute("roleUser", "flight");
                        }
                    }
                    session.setAttribute("listFunction", listFunction);
                    mess += "thanh cong";
                }
                session.setAttribute("acc", accDTO);
            } else {
                mess += "khong thanh cong";
            }
            //request.setAttribute("acc", accDTO);
            request.setAttribute("mess", mess);
            return mapping.findForward(SUCCESS);
        }

    }

    public ActionForward logout(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        session.invalidate();
        return mapping.findForward(LOGOUT);

    }
}
